<?php

declare(strict_types=1);

namespace App\Service;

use App\Model\SysSetting as SysSettingModel;

class SysSetting extends BaseService
{
    // 获取配置项
    public function getSettingItem($key)
    {
        $data = SysSettingModel::query()->where(['key' => $key])->first();
        return $data ? json_decode($data['values'], true) : null;
    }

    // 保存存储配置项
    public function saveSetting($data)
    {
        $key = $data['key'];        
        $describe = $data['describe'] ? $data['describe'] : '';
        $info = SysSettingModel::query()->where(['key' => $key])->first();
        $values = json_encode($data);
        if ($info) {
            return SysSettingModel::query()->where(['key' => $key])->update(['values' => $values]);
        } else {
            return SysSettingModel::insert(['key' => $key, 'describe' => $describe, 'values' => $values]);
        }
    }
}
